package org.sxf.app.sign.biz;

import android.content.Context;
import android.os.AsyncTask;

import com.alibaba.fastjson.JSON;
import com.lidroid.xutils.util.LogUtils;

import org.sxf.app.sign.constants.Constants;
import org.sxf.app.sign.data.DataCacheSingleton;
import org.sxf.app.sign.model.LoginRepBean;
import org.sxf.app.sign.model.LoginReqBean;
import org.sxf.app.sign.model.LogoutRepBean;
import org.sxf.app.sign.model.LogoutReqBean;
import org.sxf.app.sign.model.MsgRepBean;
import org.sxf.app.sign.provider.Client;
import org.sxf.app.sign.provider.MsgHandler;
import org.sxf.app.sign.utils.ConfigFileUtilsSingleton;
import org.sxf.app.sign.utils.SystemUtils;
import org.sxf.app.sign.utils.Utility;

import Ice.ConnectionRefusedException;
import Ice.SocketException;
import Ice.TimeoutException;
import Ice.UnknownException;

/**
 * Created by zqc on 17/1/17.
 */
public class LogoutBiz extends BaseBiz  {

    public final static int BIZ_LOGOUT_FAIL = 0x121;
    public final static int BIZ_LOGOUT_SUCCESS = 0x122;

    public final static int ERROR_LOGOUT_NET_ERROR = 0xE21;

    public LogoutBiz(Context context, OnBizCallBackListener listener){
        this.mContext = context;
        this.mBizCallBackListener = listener;
    }


    private String createLogoutReqJson(){

        LogoutReqBean bean = new LogoutReqBean();
        bean.platform = Constants.PLATFORM;
        bean.corp_id = DataCacheSingleton.getInstance().getSchoolCode();
        bean.usr_id = DataCacheSingleton.getInstance().getStudentNo();
        String reqStr = JSON.toJSONString(bean);
        LogUtils.d("msg:" + reqStr);
        return reqStr;
    }

    public void logout(){

        new AsyncTask<Void, Integer, String>() {
            @Override
            protected String doInBackground(Void... voids) {

                String reqStr = MsgHandler.msgEncode("00", createLogoutReqJson(), MsgHandler.NEED_TOKEN);

                String result = "";
                try {
                    LogUtils.d("send:" + reqStr);
                    result = Client.getServicePrx().logout(reqStr);
                    LogUtils.d("result: " + result);
                }catch (ConnectionRefusedException e){
                    LogUtils.e(Utility.getStackTrace(e));
                }catch(TimeoutException e){
                    LogUtils.e(Utility.getStackTrace(e));
                }catch (SocketException e){
                    LogUtils.e(Utility.getStackTrace(e));
                }catch (UnknownException e){
                    LogUtils.e(Utility.getStackTrace(e));
                }catch (Exception e){
                    LogUtils.e(Utility.getStackTrace(e));
                }

                return result;
            }
            @Override
            protected void onPostExecute(String result){
                if (mIsQuit){
                    return;
                }
                try {

                    if (result == null || "".equals(result)){
                        safeOnBizError(ERROR_LOGOUT_NET_ERROR, null);
                        return;
                    }

                    MsgRepBean bean = checkMsg(result, true);
                    if (bean == null){
                        safeOnBizError(ERROR_LOGOUT_NET_ERROR, null);
                        return;
                    }

                    LogoutRepBean dataBean = JSON.parseObject(bean.rd, LogoutRepBean.class);
                    if (dataBean == null){
                        safeOnBizError(ERROR_LOGOUT_NET_ERROR, null);
                        return;
                    }

                    if (dataBean.result.equals("1")){
                        safeOnBizResult(BIZ_LOGOUT_FAIL, null);
                        return;
                    }else if (!dataBean.result.equals("0")){
                        safeOnBizResult(BIZ_LOGOUT_FAIL, null);
                        return;
                    }

                    ConfigFileUtilsSingleton.getInstance().clearConfig(Constants.CONFIG_KEY_TOKEN);
                    DataCacheSingleton.getInstance().setTk("");

                    safeOnBizResult(BIZ_LOGOUT_SUCCESS, null);

                }catch (Exception e){
                    LogUtils.e(Utility.getStackTrace(e));
                    safeOnBizError(ERROR_LOGOUT_NET_ERROR, null);
                    return;
                }

                return;
            }
        }.execute();
    }


}
